<?php
/**
 * 文件说明： 点卡后台管理模块
 * ============================================================================
 * 版权所有 (C) 2010-2011 微凡工作室 | 微凡网络，并保留所有权利。
 * 网站地址: http://www.admin80.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: 微凡工作室 | 微凡网络 $
 * $Encoding: UTF-8 $
 * $Id: admin_pointcard.php  2011-3-12 下午08:53:16Z 微凡工作室 | 微凡网络 $
*/

defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin','admin',0);

class admin_pointcard extends admin {

	private $db; 
	public $userid;
	public $username;
	public $admin_email;
	public $site_id;
	private $member_db;
	private $admin_db;
	public function __construct() {
		parent::__construct();
		$this->username = param::get_cookie('admin_username');
		$this->userid = param::get_cookie('userid');
		$this->admin_email = param::get_cookie('admin_email');
		$this->site_id = $this->get_siteid();
		$this->db = pc_base::load_model('pointcard_model');
	}
	
	public function init() {
		//点卡列表
		$sql = '';
		$_GET['status'] = $_GET['status'] ? intval($_GET['status']) : 1;
		$sql = '`siteid`=\''.$this->site_id.'\'';
		switch($_GET['s']) {
			case '1': $sql .= ' AND `updatetime`=\'0\' AND (`endtime`>\''.SYS_TIME.'\' OR `endtime`=\'0\')'; break;
			case '2': $sql .= ' AND `updatetime`>\'0\''; break;
			case '3': $sql .= ' /*AND `updatetime`>\'0\'*/ AND `endtime`>\'0\' AND `endtime`<\''.SYS_TIME.'\' '; break;
		}
		$page = max(intval($_GET['page']), 1);
		$data = $this->db->listinfo($sql, '`pid` DESC', $page);
		if (!empty($data))
		{
			foreach ($data as $dkey => $dval)
			{
				$data[$dkey]['owner_username'] = $this->get_owner_username($dval['owner_uid']);
				$data[$dkey]['admin_username'] = $this->get_admin_username($dval['admin_id']);
				$data[$dkey]['format_addtime'] = $this->transfer_timestamp($dval['addtime']);
				$data[$dkey]['format_endtime'] = $this->transfer_timestamp($dval['endtime']);
				$data[$dkey]['format_updatetime'] = $this->transfer_timestamp($dval['updatetime']);
				$data[$dkey]['format_endtime'] = empty($data[$dkey]['format_endtime'])?"长期":$data[$dkey]['format_endtime'];
			}
		}		

		$big_menu = array('javascript:window.top.art.dialog({id:\'add\',iframe:\'?m=pointcard&c=admin_pointcard&a=add\', title:\''.L('pointcard_add').'\', width:\'700\', height:\'400\', lock:true}, function(){var d = window.top.art.dialog({id:\'add\'}).data.iframe;var form = d.document.getElementById(\'dosubmit\');form.click();return false;}, function(){window.top.art.dialog({id:\'add\'}).close()});void(0);', L('pointcard_add'));
		include $this->admin_tpl('pointcard_list');
	}
	
	/**
	 * 添加公告
	 */
	public function add() {
		if(isset($_POST['dosubmit'])) {
			$_POST['pointcard'] = $this->check($_POST['pointcard']);
			if($this->db->insert($_POST['pointcard'])) showmessage(L('pointcard_successful_added'), HTTP_REFERER, '', 'add');
		} else {
			//获取站点模板信息
			pc_base::load_app_func('global', 'admin');
			$siteid = $this->site_id;
			$site = pc_base::load_app_class('sites','admin');
			$info = $site->get_by_id($siteid);
			$show_header = $show_validator = $show_scroll = 1;
			pc_base::load_sys_class('form', '', 0);
			include $this->admin_tpl('pointcard_add');
		}
	}
	
	/**
	 * 修改点卡
	 */
	public function edit() {
		$_GET['pid'] = intval($_GET['pid']);
		if(!$_GET['pid']) showmessage(L('illegal_operation'));
		if ($this->check_isused($_GET['pid'])) showmessage(L('pointcard_used'));
		if(isset($_POST['dosubmit'])) {
			//$_POST['pointcard'] = $this->check($_POST['pointcard'], 'edit');
			if ($this->public_check_pwd_param($_POST['pointcard']['card_pwd'], $_GET['pid'])) showmessage(L('pointcard_pwd_exist'));
			$_POST['pointcard']['card_pwd'] = intval($_POST['pointcard']['card_pwd']);
			if ($_POST['pointcard']['card_pwd']<=0) showmessage(L('card_value_let_zero'));
			$_POST['pointcard']['endtime'] = strtotime($_POST['pointcard']['endtime']);
			$_POST['pointcard']['endtime'] = intval($_POST['pointcard']['endtime']);
			if ($_POST['pointcard']['endtime'] < SYS_TIME) $_POST['pointcard']['endtime'] = 0;
			if($this->db->update($_POST['pointcard'], array('pid' => $_GET['pid']))) showmessage(L('pointcard_a'), HTTP_REFERER, '', 'edit');
		} else {
			$where = array('pid' => $_GET['pid']);
			$pc_info = $this->db->get_one($where);
			$pc_info['format_endtime'] = $this->transfer_timestamp($pc_info['endtime']);
			pc_base::load_sys_class('form', '', 0);
			//获取站点模板信息
			pc_base::load_app_func('global', 'admin');
			$show_header = $show_validator = $show_scroll = 1;
			include $this->admin_tpl('pointcard_edit');
		}
	}
	
	/**
	 * 绑定点卡
	 */
	public function bind() {
		$_GET['pid'] = intval($_GET['pid']);
		if(!$_GET['pid']) showmessage(L('illegal_operation'));
		if ($this->check_bind($_GET['pid'])) showmessage(L('pointcard_bind_exist'));
		if ($this->check_expired($_GET['pid'])) showmessage(L('pointcard_expired'));
		if(isset($_POST['dosubmit'])) {
			$bind_username = trim(strip_tags($_POST['bind_username']));
			if (empty($bind_username)) showmessage(L('bind_username_cannot_empty'));
			$bind_uid = $this->get_bind_username_param($bind_username);			
			if($this->db->update($_POST['pointcard'], array('pid' => $_GET['pid']))) showmessage(L('pointcard_bind_ok'), HTTP_REFERER, '', 'bind');
		} else {
			$where = array('pid' => $_GET['pid']);
			$pc_info = $this->db->get_one($where);
			$pc_info['format_endtime'] = $this->transfer_timestamp($pc_info['endtime']);
			$pc_info['format_endtime'] = empty($pc_info['format_endtime'])?"长期":$pc_info['format_endtime'];
			pc_base::load_sys_class('form', '', 0);
			//获取站点模板信息
			pc_base::load_app_func('global', 'admin');
			$show_header = $show_validator = $show_scroll = 1;
			include $this->admin_tpl('pointcard_bind');
		}
	}
	
	/**
	 * ajax检测点卡卡号是否重复
	 */
	public function public_check_title() {
		if (!$_GET['card_sn']) exit(0);
		if (CHARSET=='gbk') {
			$_GET['card_sn'] = iconv('UTF-8', 'GBK', $_GET['card_sn']);
		}
		$card_sn = $_GET['card_sn'];
		if ($_GET['pid']) {
			$r = $this->db->get_one(array('pid' => $_GET['pid']));
			if ($r['card_sn'] == $card_sn) {
				exit('1');
			}
		} 
		$r = $this->db->get_one(array('siteid' => $this->site_id, 'card_sn' => $card_sn), 'pid');
		if($r['pid']) {
			exit('0');
		} else {
			exit('1');
		}
	}
	
	/**
	 * ajax检测点卡密码是否重复
	 */
	public function public_check_pwd() {
		if (!$_GET['card_pwd']) exit(0);
		if (CHARSET=='gbk') {
			$_GET['card_pwd'] = iconv('UTF-8', 'GBK', $_GET['card_pwd']);
		}
		$card_pwd = $_GET['card_pwd'];
		if ($_GET['pid']) {
			$r = $this->db->get_one(array('pid' => $_GET['pid']));
			if ($r['card_pwd'] == $card_pwd) {
				exit('1');
			}
		} 
		$r = $this->db->get_one(array('siteid' => $this->site_id, 'card_pwd' => $card_pwd), 'pid');
		if($r['pid']) {
			exit('0');
		} else {
			exit('1');
		}
	}
	
	/**
	 * 检测点卡密码是否重复
	 */
	public function public_check_pwd_param ($password = '', $pid = 0)
	{
		$password = trim(strip_tags($password));
		if (CHARSET=='gbk') {
			$password = iconv('UTF-8', 'GBK', $password);
		}
		if ($pid) {
			$r = $this->db->get_one(array('pid' => $pid));
			if ($r['card_pwd'] == $password) {
				return false;
			}
		} 
		$r = $this->db->get_one(array('siteid' => $this->site_id, 'card_pwd' => $password), 'pid');
		if($r['pid']) {
			return true;
		} else {
			return false;
		}
		return false;
	}
	
	/**
	 * ajax获取要绑定用户的ID
	 */
	public function get_bind_username ()
	{
		$bind_uname = trim(strip_tags($_GET['bind_username']));
		if (empty($bind_uname)) exit(0);	
		if (CHARSET=='gbk') {
			$bind_uname = iconv('UTF-8', 'GBK', $bind_uname);
		}
		$this->member_db = pc_base::load_model('member_model');
		$r = $this->member_db->get_one(array('username' => $bind_uname), 'userid');
		if($r['userid']) {
			exit($r['userid']);
		} else {
			exit('0');
		}
	}
	
	/**
	 * 获取要绑定用户的ID
	 */
	public function get_bind_username_param ($username = '')
	{
		$bind_uid = 0;
		$bind_uname = trim(strip_tags($username));
		if (empty($bind_uname)) return $bind_uid;	
		if (CHARSET=='gbk') {
			$bind_uname = iconv('UTF-8', 'GBK', $bind_uname);
		}
		$this->member_db = pc_base::load_model('member_model');
		$r = $this->member_db->get_one(array('username' => $bind_uname), 'userid');
		if($r['userid']) {
			$bind_uid = $r['userid'];
		}
		return $bind_uid;
	}
	
	/**
	 * 验证表单数据
	 * @param  		array 		$data 表单数组数据
	 * @param  		string 		$a 当表单为添加数据时，自动补上缺失的数据。
	 * @return 		array 		验证后的数据
	 */
	private function check($data = array(), $a = 'add') {
		if($data['card_sn']=='') showmessage(L('title_cannot_empty'));
		if($data['card_pwd']=='') showmessage(L('no_input_card_pwd'));
		if($data['card_value']=='') showmessage(L('no_input_card_value'));
		$data['card_value'] = intval($data['card_value']);
		if ($data['card_value']<=0) showmessage(L('card_value_let_zero'));
		$r = $this->db->get_one(array('card_sn' => $data['card_sn']));
		$pe = $this->db->get_one(array('card_pwd' => $data['card_pwd']));
		if ($a=='add') {
			if (is_array($r) && !empty($r)) {
				showmessage(L('pointcard_exist'), HTTP_REFERER);
			}
			if (is_array($pe) && !empty($pe)) {
				showmessage(L('pointcard_pwd_exist'), HTTP_REFERER);
			}
			$data['siteid'] = $this->site_id;
			$data['owner_uid'] = 0;
			$data['addtime'] = SYS_TIME;
			$data['updatetime'] = 0;
			$data['endtime'] = strtotime($data['endtime']);
			$data['endtime'] = intval($data['endtime']);
			if ($data['endtime'] < SYS_TIME) $data['endtime'] = 0;
			$data['admin_id'] = $this->userid;
		} else {
			if ($r['pid'] && ($r['pid']!=$_GET['pid'])) {
				showmessage(L('pointcard_exist'), HTTP_REFERER);
			}
		}
		return $data;
	}
	
	/**
	 * 获得所有者的用户名
	 * 
	 * @param int $uid
	 */
	private function get_owner_username($uid = 0)
	{
		$member_username = '';
		$members = array();
		$this->member_db = pc_base::load_model('member_model');
		$members = $this->member_db->get_one(array('userid' => $uid), 'username');
		if (!empty($members))
		{
			$member_username = $members['username'];
		}		
		return $member_username;
	}
	
	/**
	 * 获得管理员的用户名
	 * 
	 * @param int $uid
	 */
	private function get_admin_username($admin_id = 0)
	{
		$admin_username = '';
		$admins = array();
		$this->admin_db = pc_base::load_model('admin_model');
		$admins = $this->admin_db->get_one(array('userid' => $admin_id), 'username');
		if (!empty($admins))
		{
			$admin_username = $admins['username'];
		}		
		return $admin_username;
	}
	
	/**
	 * 转换数字时间串到可识别的日期串
	 * 
	 * @param int $timestamp
	 */
	private function transfer_timestamp ($timestamp = 0, $format = "Y-m-d H:i:s")
	{
		$time_date = '';
		if ($timestamp>0)
		{
			$time_date = date($format, $timestamp);
		}	
		return $time_date;
	}
	
	/**
	 * 检查点卡是否已经绑定
	 */
	private function check_bind($pid = 0)
	{
		if ($pid>0)
		{
			$r = $this->db->get_one(array('pid'=>$pid), 'owner_uid');
			if ($r['owner_uid']>0) return true;
		}
		return false;
	}
	
	/**
	 * 检查点卡是否已经过期
	 */
	private function check_expired($pid = 0)
	{
		if ($pid>0)
		{
			$r = $this->db->get_one(array('pid'=>$pid), 'endtime');
			if ($r['endtime']>0 && $r['endtime']<SYS_TIME) return true;
		}
		return false;
	}
	
	/**
	 * 检查点卡是否已经使用
	 */
	private function check_isused($pid = 0)
	{
		if ($pid>0)
		{
			$r = $this->db->get_one(array('pid'=>$pid), 'updatetime');
			if ($r['updatetime']>0) return true;
		}
		return false;
	}
}
?>